pip install opencv-python
Requirement already satisfied: opencv-python in c:\users\jujun\anaconda3\lib\site-packages (4.9.0.80)Note: you may need to restart the kernel to use updated packages. Requirement already satisfied: numpy>=1.21.2 in c:\users\jujun\anaconda3\lib\site-packages (from opencv-python) (1.24.3)
import cv2 #cv2 is an open source computer vision/image
import matplotlib.pyplot as plt
#empty list for the class labels
classLabels = []
#file path for the labels.txt file
file_name = r'C:\Users\jujun\OneDrive\Desktop\Object Detector Files\Labels.txt'
#this code will read the class labels from the labels.txt file
with open(file_name, 'r') as fpt:
classLabels = fpt.read().rstrip('\n').split('\n')
#file path of the configuration file and the frozen model file
config_file = r'C:\Users\jujun\OneDrive\Desktop\Object Detector\ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
frozen_model = r'C:\Users\jujun\OneDrive\Desktop\Object Detector\frozen_inference_graph.pb'
#create model using the configuration file and the frozen model file above
model = cv2.dnn_DetectionModel(frozen_model, config_file)
#this will print the texts or the labels inside the labels.txt file
print(classLabels)
['person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'sofa', 'pottedplant', 'bed', 'diningtable', 'toilet', 'tvmonitor', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
#this will print the length of the file
print(len(classLabels))
80
model.setInputSize(320, 320) #size
model.setInputScale(1.0 / 127.5) #scale
model.setInputMean((127.5, 127.5, 127.5)) #mean
model.setInputSwapRB(True) #swapr&b
< cv2.dnn.Model 000002C0E7ACCE90>
#This code will read the image of the given file
img = cv2.imread(r'C:\Users\jujun\OneDrive\Desktop\Object Detector Files\R.png')
if img is not None:
plt.imshow(img)
plt.show()
else:
print("Error loading the image.")
#this will display the image using Matplotlib after converting from BGR to RGB color space
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0e7b950d0>
#this detect objects in the input image using the trained model.
ClassIndex, confidence, bbox = model.detect(img, confThreshold=0.5)
print(ClassIndex)
[1]
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
print(f"Class Index: {ClassInd}, Confidence: {conf}, Bounding Box: {box}")
Class Index: 1, Confidence: 0.7664116024971008, Bounding Box: [ 644 44 652 1036]
# Assuming ClassIndex, confidence, and bbox are obtained from the detection
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
x, y, w, h = box
color = (255, 0, 0) # BGR color format, so (255, 0, 0) is blue
#this will rectangle
cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
#this will put text
label = f"Class: {ClassInd}, Confidence: {conf:.2f}"
cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
font_scale = 3 #scale
font = cv2.FONT_HERSHEY_PLAIN #font
for ClassInd, conf, boxes in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
# cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# cv2.putText(img, text, (text_offset_x, text_offset_y), font, fontScale=font_scale, color=(0, 0, 0), thickness=1)
cv2.rectangle(img, boxes, (255, 0, 0), 2)
cv2.putText(img, classLabels[ClassInd-1], (boxes[0]+10, boxes[1]+40), font, fontScale=font_scale, color=(0, 255, 0), thickness=3)
#this will print the final result/the detected image
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0eb26bf50>
#THE SECOND IMAGE
#This code will read the image of the given file
img = cv2.imread(r'C:\Users\jujun\OneDrive\Desktop\Object Detector Files\yellow_cayman_gt4_decals-600x400.jpg')
if img is not None:
plt.imshow(img)
plt.show()
else:
print("Error loading the image.")
#this will display the image using Matplotlib after converting from BGR to RGB color space
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0ea4f8110>
ClassIndex, confidence, bbox = model.detect(img, confThreshold=0.5)
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
print(f"Class Index: {ClassInd}, Confidence: {conf}, Bounding Box: {box}")
Class Index: 3, Confidence: 0.6810831427574158, Bounding Box: [215 138 168 115]
# Assuming ClassIndex, confidence, and bbox are obtained from the detection
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
x, y, w, h = box
color = (255, 0, 0) # BGR color format, so (255, 0, 0) is blue
#this will draw rectangle
cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
#this will put text
label = f"Class: {ClassInd}, Confidence: {conf:.2f}"
cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
font_scale = 3
font = cv2.FONT_HERSHEY_PLAIN
for ClassInd, conf, boxes in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
# cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# cv2.putText(img, text, (text_offset_x, text_offset_y), font, fontScale=font_scale, color=(0, 0, 0), thickness=1)
cv2.rectangle(img, boxes, (255, 0, 0), 2)
cv2.putText(img, classLabels[ClassInd-1], (boxes[0]+10, boxes[1]+40), font, fontScale=font_scale, color=(0, 255, 0), thickness=3)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0ea4f0110>
#THE THIRD IMAGE
#this will read the image
img = cv2.imread(r'C:\Users\jujun\OneDrive\Desktop\Object Detector Files\OIP.jpg')
if img is not None:
plt.imshow(img)
plt.show()
else:
print("Error loading the image.")
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0ea577fd0>
ClassIndex, confidence, bbox = model.detect(img, confThreshold=0.5)
print(ClassIndex)
[2]
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
print(f"Class Index: {ClassInd}, Confidence: {conf}, Bounding Box: {box}")
Class Index: 2, Confidence: 0.8012720942497253, Bounding Box: [ 130 133 1351 765]
# Assuming ClassIndex, confidence, and bbox are obtained from the detection
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
x, y, w, h = box
color = (255, 0, 0) # BGR color format, so (255, 0, 0) is blue
# Draw rectangle
cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
# Put text
label = f"Class: {ClassInd}, Confidence: {conf:.2f}"
cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
font_scale = 3
font = cv2.FONT_HERSHEY_PLAIN
for ClassInd, conf, boxes in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
# cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# cv2.putText(img, text, (text_offset_x, text_offset_y), font, fontScale=font_scale, color=(0, 0, 0), thickness=1)
cv2.rectangle(img, boxes, (255, 0, 0), 2)
cv2.putText(img, classLabels[ClassInd-1], (boxes[0]+10, boxes[1]+40), font, fontScale=font_scale, color=(0, 255, 0), thickness=3)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0ea6a1310>
#THE FOURTH IMAGE
# read an image
img = cv2.imread(r'C:\Users\jujun\OneDrive\Desktop\Object Detector Files\F122_9773.jpg')
if img is not None:
plt.imshow(img)
plt.show()
else:
print("Error loading the image.")
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0ea76b910>
ClassIndex, confidence, bbox = model.detect(img, confThreshold=0.5)
print(ClassIndex)
[ 1 10 10 3 3 3 3 3 3 10 3 10 10 3 3 10 3 3 10]
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
print(f"Class Index: {ClassInd}, Confidence: {conf}, Bounding Box: {box}")
Class Index: 1, Confidence: 0.7701712846755981, Bounding Box: [1119 882 174 329] Class Index: 10, Confidence: 0.7329203486442566, Bounding Box: [113 190 313 691] Class Index: 10, Confidence: 0.6703238487243652, Bounding Box: [972 471 69 136] Class Index: 3, Confidence: 0.6687649488449097, Bounding Box: [454 759 109 65] Class Index: 3, Confidence: 0.6530338525772095, Bounding Box: [423 762 77 65] Class Index: 3, Confidence: 0.6260266304016113, Bounding Box: [770 713 127 75] Class Index: 3, Confidence: 0.592012345790863, Bounding Box: [489 720 114 52] Class Index: 3, Confidence: 0.5917191505432129, Bounding Box: [856 701 109 85] Class Index: 3, Confidence: 0.5893480181694031, Bounding Box: [927 688 123 103] Class Index: 10, Confidence: 0.5814199447631836, Bounding Box: [1673 322 92 133] Class Index: 3, Confidence: 0.5799921154975891, Bounding Box: [ 0 847 374 257] Class Index: 10, Confidence: 0.5541713237762451, Bounding Box: [1294 562 39 82] Class Index: 10, Confidence: 0.5522180795669556, Bounding Box: [389 914 181 287] Class Index: 3, Confidence: 0.5517853498458862, Bounding Box: [722 738 163 72] Class Index: 3, Confidence: 0.5387980341911316, Bounding Box: [423 721 56 41] Class Index: 10, Confidence: 0.5313695669174194, Bounding Box: [127 330 233 593] Class Index: 3, Confidence: 0.522835373878479, Bounding Box: [687 743 116 73] Class Index: 3, Confidence: 0.5091461539268494, Bounding Box: [700 717 79 47] Class Index: 10, Confidence: 0.5010348558425903, Bounding Box: [1701 313 72 124]
# Assuming ClassIndex, confidence, and bbox are obtained from the detection
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
x, y, w, h = box
color = (255, 0, 0) # BGR color format, so (255, 0, 0) is blue
# Draw rectangle
cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
# Put text
label = f"Class: {ClassInd}, Confidence: {conf:.2f}"
cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
font_scale = 3
font = cv2.FONT_HERSHEY_PLAIN
for ClassInd, conf, boxes in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
# cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# cv2.putText(img, text, (text_offset_x, text_offset_y), font, fontScale=font_scale, color=(0, 0, 0), thickness=1)
cv2.rectangle(img, boxes, (255, 0, 0), 2)
cv2.putText(img, classLabels[ClassInd-1], (boxes[0]+10, boxes[1]+40), font, fontScale=font_scale, color=(0, 255, 0), thickness=3)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0ea5dd290>
#THE FIFTH IMAGE
# read an image
img = cv2.imread(r'C:\Users\jujun\OneDrive\Desktop\Object Detector Files\rsz_young_peoples_needs.jpg')
if img is not None:
plt.imshow(img)
plt.show()
else:
print("Error loading the image.")
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0ea955d10>
ClassIndex, confidence, bbox = model.detect(img, confThreshold=0.5)
print(ClassIndex)
[1 1 1 1 1 1]
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
print(f"Class Index: {ClassInd}, Confidence: {conf}, Bounding Box: {box}")
Class Index: 1, Confidence: 0.7777454257011414, Bounding Box: [ 335 810 1187 1951] Class Index: 1, Confidence: 0.7595170736312866, Bounding Box: [1335 855 1117 1914] Class Index: 1, Confidence: 0.7211544513702393, Bounding Box: [ 578 104 1543 1555] Class Index: 1, Confidence: 0.7065383791923523, Bounding Box: [2744 701 1083 2078] Class Index: 1, Confidence: 0.6683363914489746, Bounding Box: [ 530 406 1505 2008] Class Index: 1, Confidence: 0.6274985074996948, Bounding Box: [2061 583 839 2076]
# Assuming ClassIndex, confidence, and bbox are obtained from the detection
for ClassInd, conf, box in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
x, y, w, h = box
color = (255, 0, 0) # BGR color format, so (255, 0, 0) is blue
# Draw rectangle
cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
# Put text
label = f"Class: {ClassInd}, Confidence: {conf:.2f}"
cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
font_scale = 3
font = cv2.FONT_HERSHEY_PLAIN
for ClassInd, conf, boxes in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
# cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# cv2.putText(img, text, (text_offset_x, text_offset_y), font, fontScale=font_scale, color=(0, 0, 0), thickness=1)
cv2.rectangle(img, boxes, (255, 0, 0), 2)
cv2.putText(img, classLabels[ClassInd-1], (boxes[0]+10, boxes[1]+40), font, fontScale=font_scale, color=(0, 255, 0), thickness=3)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
<matplotlib.image.AxesImage at 0x2c0ea9c9310>